#!/usr/local/bin/python2.7

import copy, random, string
from collections import OrderedDict

class Component(object):

    def __init__(self):
        self._gen = OrderedDict()
        self._mech = OrderedDict()
        self._elec = OrderedDict()
        self._iandc = OrderedDict()

        self._dicts = {'gen':self._gen, 'mech':self._mech, \
                       'elec':self._elec, 'iandc':self._iandc}
        self._dict_keys = {}
        self._dict_keys['gen'] = ['SystemName', 'SystemNumber', \
            'UniqueIDNumber', 'ItemName', 'BuildingNumber', 'Location', \
            'Drawings', 'CriticalComponent', 'AdditionalIdentifiers', 'Comments']
        self._dict_keys['mech'] = ['To', 'From', 'Manufacturer', \
            'ModelNumber', 'SerialNumber', 'Size', 'FlowRates', 'RPM', \
            'Material']
        self._dict_keys['elec'] = ['To', 'From', 'Manufacturer', \
            'ModelNumber', 'SerialNumber', 'HorsePower', 'RPM', 'Volts', \
            'Hertz', 'Phase', 'Amps']
        self._dict_keys['iandc'] = ['To', 'From', 'Manufacturer', \
            'ModelNumber', 'SerialNumber', 'OutputRange', 'DisplayRange', \
            'Medium', 'AnalogorDigital']

        self._randID = str(int(round(random.random() * 10**5, 0)))

        self._nomatches = []
        self._pics = []
        
        self._name = ''
        self._path_to_root = ''
        self._path_from_root = ''

        self._currdict = self._gen
        self._buildDefaults()

    def getToRoot(self):
        return self._path_to_root
    def setToRoot(self, val):
        self._path_to_root = val
    path_to_root = property(getToRoot, setToRoot)
    def getFromRoot(self):
        return self._path_from_root
    def setFromRoot(self, val):
        self._path_from_root = val
    path_from_root = property(getFromRoot, setFromRoot)

    def getValue(self, dict_name, key):
        if not(dict_name in self._dicts):
            raise Exception('Invalid dictionary name.')
        
        if not(key in self._dicts[dict_name]):
            raise Exception('Invalid key for component dictionary.')

        des_dict = self._dicts[dict_name]

        return des_dict[key]

    def _buildDefaults(self):
        for name, prop_dict in self._dicts.items():
            for key in self._dict_keys[name]:
                self._dicts[name][key] = []

    def getName(self):
        if self._name == '':
            sys_num = string.join(self._gen['SystemNumber'])
            uniqid = string.join(self._gen['UniqueIDNumber'])
            
            if len(sys_num) == 0: sys_num = 'NoNum'
            if len(uniqid) == 0: uniqid = 'NoID'

            uniqid = self._removeBadChars(uniqid)
            sys_num = self._removeBadChars(sys_num)

            self._name += uniqid + '_'
            self._name += sys_num + '_'
            self._name += self._randID

        return self._name
    name = property(getName)

    def _removeBadChars(self, text):
        bad_chars = '@#$%^&*()~`:;\"\'{[}]|\\/?,<>+='
        return ''.join([c for c in text if not(c in bad_chars)])

    def getDict(self, name):
        if not(name in self._dicts):
            raise Exception('Invalid dictionary name.')
        
        return copy.deepcopy(self._dicts[name])

    def addPic(self, pic):
        self._pics.append(pic)

    def getPics(self):
        return copy.deepcopy(self._pics)

    def setCurrentDict(self, name):
        cat_mapper = {'Mechanical':'_mech', 'Electrical':'_elec', \
                            'IandC':'_iandc'}
        self._currdict = self.__dict__[cat_mapper[name]]

    def addField(self, name, vals):
        self._currdict[name] = vals

    def addNoMatch(self, field_name, val):
        if val is None: val = ''
        self._nomatches.append([field_name, val])

    def __str__(self):
        mystr = ''
        mystr += 'name: ' + self.name
        mystr += '\n'
        mystr += 'path to root: ' + self._path_to_root
        mystr += '\n'
        mystr += 'path from root: ' + self._path_from_root
        mystr += '\n'

        mystr += 'pics:'
        mystr += '\n'
        for pic in self._pics:
            mystr += '    '
            mystr += 'name: ' + pic.name + ', path: ' + pic.path
            mystr += '\n'

        mystr += 'gen: ' + str(self._gen)
        mystr += '\n'
        mystr += '\n'
        mystr += 'mech: ' + str(self._mech)
        mystr += '\n'
        mystr += '\n'
        mystr += 'elec: ' + str(self._elec)
        mystr += '\n'
        mystr += '\n'
        mystr += 'iandc: ' + str(self._iandc)
        mystr += '\n'

        return mystr

class Pic(object):
    def __init__(self, name, path):
        self.name = name
        self.path = path

